var express = require('express');
var router = express.Router();

var mysql = require('mysql'); //引入mysql 模块


var pool = mysql.createPool({
  host: 'localhost',
  port:'3306',
  user: 'root',
  password: '123456',
  database: 'todolist'
});

/* GET users listing. */
router.get('/', function(req, res, next) {
	
//我要执行的mysql语句
var sql = 'select * from todolist';
console.log("sql:"+sql);

pool.getConnection(function(err, connection) {
    if(err) {
		console.log(err)
		console.log('MySQL数据库建立连接失败。');
		
	}
    else{
        console.log('数据库建立连接成功。');
        connection.query( sql, function(err,data) {
            if(err) 
				console.log('查询数据操作失败。');
            else{
                console.log(data);
			
				res.end(JSON.stringify({
        data: data
      }));

            }
			 connection.release();
        });
    }
});				

});

module.exports = router;
